package com.gmcc.docmanager.util;

import java.io.File;

import android.content.Intent;
import android.net.Uri;

public class IntentUtils {

	public static Intent getOpenFileIntent(String filePath) {
		int type = filePath.lastIndexOf(".");
		if (type >= 0) {
			return getOpenFileIntent(filePath, filePath.substring(type + 1));
		}
		return null;
	}

	public static Intent getOpenFileIntent(String filePath, String type) {
		if ("doc".equalsIgnoreCase(type) || "docx".equalsIgnoreCase(type)) {
			return getWordFileIntent(filePath);
		} else if ("ppt".equalsIgnoreCase(type)
				|| "pptx".equalsIgnoreCase(type)) {
			return getPptFileIntent(filePath);
		} else if ("xls".equalsIgnoreCase(type)
				|| "xlsx".equalsIgnoreCase(type)) {
			return getExcelFileIntent(filePath);
		} else if ("pdf".equalsIgnoreCase(type)) {
			return getPdfFileIntent(filePath);
		} else if ("mpeg".equalsIgnoreCase(type)
				|| "avi".equalsIgnoreCase(type)) {
			return getVideoFileIntent(filePath);
		}
		return null;
	}

	public static Intent getPdfFileIntent(String param) {
		Intent intent = new Intent("android.intent.action.VIEW");
		intent.addCategory("android.intent.category.DEFAULT");
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		Uri uri = Uri.fromFile(new File(param));
		intent.setDataAndType(uri, "application/pdf");
		return intent;
	}

	public static Intent getVideoFileIntent(String param) {
		Intent intent = new Intent("android.intent.action.VIEW");
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		intent.putExtra("oneshot", 0);
		intent.putExtra("configchange", 0);
		Uri uri = Uri.fromFile(new File(param));
		intent.setDataAndType(uri, "video/*");
		return intent;
	}

	public static Intent getWordFileIntent(String param) {
		Intent intent = new Intent("android.intent.action.VIEW");
		intent.addCategory("android.intent.category.DEFAULT");
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		Uri uri = Uri.fromFile(new File(param));
		intent.setDataAndType(uri, "application/msword");
		return intent;
	}

	public static Intent getExcelFileIntent(String param) {
		Intent intent = new Intent("android.intent.action.VIEW");
		intent.addCategory("android.intent.category.DEFAULT");
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		Uri uri = Uri.fromFile(new File(param));
		intent.setDataAndType(uri, "application/vnd.ms-excel");
		return intent;
	}

	public static Intent getPptFileIntent(String param) {
		Intent intent = new Intent("android.intent.action.VIEW");
		intent.addCategory("android.intent.category.DEFAULT");
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		Uri uri = Uri.fromFile(new File(param));
		intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
		return intent;
	}

}
